package cn.anc.aonicardv.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import java.util.Locale;

/**
 * Created by yangdai on 2018/3/12.
 */

public class AppUtils {

    public static boolean appIsInstall(Context context, String packageName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo == null ? false : true;
    }

    public static void updateAppLanguage(Context context, Locale locale) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.locale = locale;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        context.getResources().updateConfiguration(configuration, metrics);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static Context updateResources(Context context,Locale locale) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }



}
